package com.gzhryc.common.event;

import com.gzhryc.common.GenericsTools;

/**
 * 延迟处理接口
 * @author 张浩
 * @version 2017-1-11
 */
public abstract class AbsDelayHandler<T extends IDelayEvent> {
	
	private Class<T> clazz;
	
	private AbsDelayHandler<?> nextHandler;
	
	@SuppressWarnings("unchecked")
	public AbsDelayHandler() {
		clazz = (Class<T>)GenericsTools.getSuperClassGenericType(this.getClass());
	}
	
	public boolean intercept(IDelayEvent event) {
		boolean flag = false;
		if(this.nextHandler != null) {
			flag = nextHandler.intercept(event);
		}
		if(!flag && clazz.isInstance(event)) {
			flag = execute(clazz.cast(event));
		}
		return flag;
	}
	
	public AbsDelayHandler<?> getNextHandler(){
		return this.nextHandler;
	}
	
	public void setNextHandler(AbsDelayHandler<?> handler) {
		if(this.nextHandler != null) {
			this.nextHandler.setNextHandler(handler);
		}else {
			this.nextHandler = handler;
		}
	}

	/**
	 * 处理延迟事件
	 * @param event
	 * @return false:事件继续传递，true:事件已处理，不继续传递
	 */
	public abstract boolean execute(T event);

}
